﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.Admin.AdvertManager
{
    public partial class UserAdPositionList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindAdvertOrder();
                LoadContent(false);

                // 从BasePage中得到
                PageLiteral.Text = BuildPageLinks();
            }
        }


        #region 加载列表
        private void LoadContent(bool mark)
        {

            //设置控件属性，从BasePage中得到
            PageConfig(AdvertUPager);

            LoadCondition();

            string adpositionid = ddlAdPosition.SelectedValue;
            string nickname = txtNickName.Text.Trim();
            string begindate = txtBeginDate.Text.Trim();
            string enddate = txtEndDate.Text.Trim();

            //List<Member> list = new List<Member();
            UserAdPositionHelper uaphelper = new UserAdPositionHelper();
            Criteria c = new Criteria();

            if (adpositionid != "-1")
            {
                c.Add(CriteriaType.Equals, "AdPositionID", adpositionid);
            }
            if (!string.IsNullOrEmpty(nickname))
            {
                c.Add(CriteriaType.Like, "NickName", "%" + nickname + "%");
            }
            if (!string.IsNullOrEmpty(begindate))
            {
                DateTime dtbegindate=Convert.ToDateTime(begindate);
                c.Add(CriteriaType.MoreThanEquals, "BeginDate", dtbegindate);
            }
            if (!string.IsNullOrEmpty(enddate))
            {
                DateTime dtenddate = Convert.ToDateTime(enddate);
                c.Add(CriteriaType.LessThanEquals, "EndDate", dtenddate);
            }

            AdvertUPager.ItemCount = uaphelper.QueryUserAdPositionCountBySearch(c);

            // 以下的值从BasePage中得到
            AdvertUPager.UrlFormat = UrlFormat;
            AdvertUPager.PrefixText = GetPreFixText(AdvertUPager);

            if (mark)
            {
                PagerInit(AdvertUPager);
            }

            string[] fields = new string[] { "ID", "AdPositionID", "AdPositionName", "Orders", "UsersID", "NickName", "BeginDate", "EndDate", "CreateDate" };

            DataTable dt = new DataTable();
            dt = uaphelper.QueryUserAdPositionBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, AdvertUPager.Begin - 1, AdvertUPager.Count, fields);


            rptAdvert.DataSource = dt;
            rptAdvert.DataBind();
        }
        #endregion

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            SetCondition();
            LoadContent(true);
        }


        #region 查询条件设置
        private void SetCondition()
        {

            int adpositionid = Convert.ToInt32(ddlAdPosition.SelectedValue);
            string nickname = txtNickName.Text.Trim();
            string begindate = txtBeginDate.Text.Trim();
            string enddate = txtEndDate.Text.Trim();

            //将查询条件放入Session中
            Session["QueryList"] = adpositionid + "@" + nickname + "@" +begindate+"@"+enddate+"@"+ "UserAdPositionList";

        }

        private void LoadCondition()
        {
            string condition = Session["QueryList"] + "";

            string[] conditionAll = condition.Split('@');
            if (conditionAll.Length == 5)
            {
                string page = conditionAll[4];
                if (page == "UserAdPositionList")
                {
                    ddlAdPosition.SelectedValue = conditionAll[0];
                    txtNickName.Text = conditionAll[1];
                    txtBeginDate.Text = conditionAll[2];
                    txtEndDate.Text = conditionAll[3];
                }
            }
        }
        #endregion

        #region 加载广告位
        private void BindAdvertOrder()
        {
            AdPositionHelper aphelper = new AdPositionHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "Flag", 0);
            c.Add(CriteriaType.Equals, "Type", 1);

            List<AdPosition> list = aphelper.QueryAdPositionBySearch(c, new Order[] { new Order("Orders", OrderMode.Asc) });

            ddlAdPosition.DataSource = list;
            ddlAdPosition.DataTextField = "AdPositionName";
            ddlAdPosition.DataValueField = "ID";
            ddlAdPosition.DataBind();

            ListItem li = new ListItem();
            li.Text = "-请选择-";
            li.Value = "-1";
            ddlAdPosition.Items.Insert(0, li);
        }
        #endregion
    }
}